package com.tfswx.ruleengine.database.bean.table;

import com.tfswx.ruleengine.compute.exception.AppException;
import com.tfswx.ruleengine.database.config.DataSourceConfig;
import com.tfswx.ruleengine.database.service.GzyqService;

import java.util.*;

/**
 * @author Ricky
 * @date 2021/7/10
 */

public class TableManager {

    private TableManager() {
    }

    private static final Map<String, Table> tableMap = new HashMap<>(3);

    static {
        // 规则分类表
        Table gzflTable = new Table("T_GZYQ_XT_GZFL", "规则分类表");
        tableMap.put("gzfl", gzflTable);
        List<TabeleField> gzflFieldList = new ArrayList<>();
        gzflTable.setFieldList(gzflFieldList);
        gzflFieldList.add(new TabeleField("GZFLBM", Zdlx.CHAR, 32, true, "规则分类编码"));
        gzflFieldList.add(new TabeleField("GZFLMC", Zdlx.VARCHAR, 300, false, "规则分类名称"));
        gzflFieldList.add(new TabeleField("GZFLBS", Zdlx.VARCHAR, 300, false, "规则分类标识"));
        gzflFieldList.add(new TabeleField("XMBM", Zdlx.CHAR, 32, false, "项目编码"));
        gzflFieldList.add(new TabeleField("SFSC", Zdlx.CHAR, 1, false, "是否删除"));
        gzflFieldList.add(new TabeleField("CJSJ", Zdlx.TIMESTAMP, null, false, "创建时间"));
        gzflFieldList.add(new TabeleField("ZHXGSJ", Zdlx.TIMESTAMP, null, false, "最后修改时间"));

        // 规则定义表
        Table gzdyTable = new Table("T_GZYQ_XT_GZDY", "规则定义表");
        tableMap.put("gzdy", gzdyTable);
        List<TabeleField> gzdyFieldList = new ArrayList<>();
        gzdyTable.setFieldList(gzdyFieldList);
        gzdyFieldList.add(new TabeleField("GZBM", Zdlx.CHAR, 32, true, "规则编码"));
        gzdyFieldList.add(new TabeleField("GZMC", Zdlx.VARCHAR, 300, false, "规则名称"));
        gzdyFieldList.add(new TabeleField("GZMS", Zdlx.VARCHAR, 4000, false, "规则描述"));
        gzdyFieldList.add(new TabeleField("GZFLBM", Zdlx.CHAR, 32, false, "规则分类编码"));
        gzdyFieldList.add(new TabeleField("GZBDS", Zdlx.VARCHAR, 4000, false, "规则表达式"));
        gzdyFieldList.add(new TabeleField("BMJH", Zdlx.VARCHAR, 1000, false, "表名集合"));
        gzdyFieldList.add(new TabeleField("ZDJH", Zdlx.VARCHAR, 1000, false, "字段集合"));
        gzdyFieldList.add(new TabeleField("SFQY", Zdlx.CHAR, 1, false, "是否启用"));
        gzdyFieldList.add(new TabeleField("GZPZ", Zdlx.VARCHAR, 4000, false, "规则配置"));
//        gzdyFieldList.add(new TabeleField("GZLX", Zdlx.CHAR, 1, false, "规则类型"));
//        gzdyFieldList.add(new TabeleField("WTLX", Zdlx.CHAR, 1, false, "问题类型"));
//        gzdyFieldList.add(new TabeleField("XGJY", Zdlx.VARCHAR, 300, false, "修改建议"));
        gzdyFieldList.add(new TabeleField("PX", Zdlx.INTEGER, 13, false, "排序"));
        gzdyFieldList.add(new TabeleField("SFSC", Zdlx.CHAR, 1, false, "是否删除"));
        gzdyFieldList.add(new TabeleField("CJSJ", Zdlx.TIMESTAMP, null, false, "创建时间"));
        gzdyFieldList.add(new TabeleField("ZHXGSJ", Zdlx.TIMESTAMP, null, false, "最后修改时间"));

        // 表达式表
        Table bdsTable = new Table("T_GZYQ_XT_BDS", "表达式表");
        tableMap.put("bds", bdsTable);
        List<TabeleField> bdsFieldList = new ArrayList<>();
        bdsTable.setFieldList(bdsFieldList);
        bdsFieldList.add(new TabeleField("BDSBM", Zdlx.CHAR, 32, true, "表达式编码"));
        bdsFieldList.add(new TabeleField("GZBM", Zdlx.CHAR, 32, false, "规则编码"));
        bdsFieldList.add(new TabeleField("BDSMC", Zdlx.VARCHAR, 10, false, "表达式名称"));
        bdsFieldList.add(new TabeleField("BDSMS", Zdlx.VARCHAR, 4000, false, "表达式描述"));
        bdsFieldList.add(new TabeleField("BDS", Zdlx.VARCHAR, 4000, false, "表达式"));
        bdsFieldList.add(new TabeleField("HCZD", Zdlx.VARCHAR, 4000, false, "核查字段"));
        bdsFieldList.add(new TabeleField("BDSLX", Zdlx.VARCHAR, 10, false, "表达式类型"));
        bdsFieldList.add(new TabeleField("SFSC", Zdlx.CHAR, 1, false, "是否删除"));
        bdsFieldList.add(new TabeleField("CJSJ", Zdlx.TIMESTAMP, null, false, "创建时间"));
        bdsFieldList.add(new TabeleField("ZHXGSJ", Zdlx.TIMESTAMP, null, false, "最后修改时间"));

        // 模型实体关系表
        Table mxstgxTable = new Table("T_GZYQ_XT_MXSTGX", "模型实体关系表");
        tableMap.put("mxstgx", mxstgxTable);
        List<TabeleField> mxstgxFieldList = new ArrayList<>();
        mxstgxTable.setFieldList(mxstgxFieldList);
        mxstgxFieldList.add(new TabeleField("MXSTGXBM", Zdlx.CHAR, 32, true, "模型实体关系编码"));
        mxstgxFieldList.add(new TabeleField("XMBM", Zdlx.CHAR, 32, false, "项目编码"));
        mxstgxFieldList.add(new TabeleField("MXBM", Zdlx.VARCHAR, 36, false, "模型编码"));
        mxstgxFieldList.add(new TabeleField("ZSTBM", Zdlx.VARCHAR, 50, false, "主实体表名"));
        mxstgxFieldList.add(new TabeleField("CSTBM", Zdlx.VARCHAR, 50, false, "从实体表名"));
        mxstgxFieldList.add(new TabeleField("GXBM", Zdlx.VARCHAR, 36, false, "关系编码"));
        mxstgxFieldList.add(new TabeleField("GLGX", Zdlx.VARCHAR, 1000, false, "关联关系"));
        mxstgxFieldList.add(new TabeleField("GLLX", Zdlx.CHAR, 1, false, "关联类型1.一对一2.一对多"));
        mxstgxFieldList.add(new TabeleField("MXSTLX", Zdlx.CHAR, 1, false, "模型实体类型1:数据主表2:业务主表3:关联表"));
        mxstgxFieldList.add(new TabeleField("SFSC", Zdlx.CHAR, 1, false, "是否删除"));
        mxstgxFieldList.add(new TabeleField("CJSJ", Zdlx.TIMESTAMP, null, false, "创建时间"));
        mxstgxFieldList.add(new TabeleField("ZHXGSJ", Zdlx.TIMESTAMP, null, false, "最后修改时间"));

        // 表达式表
        Table stgxTable = new Table("T_GZYQ_XT_STGX", "实体关系表");
        tableMap.put("stgx", bdsTable);
        List<TabeleField> stgxFieldList = new ArrayList<>();
        stgxTable.setFieldList(stgxFieldList);
        stgxFieldList.add(new TabeleField("GXBM", Zdlx.CHAR, 32, true, "关系编码"));
        stgxFieldList.add(new TabeleField("FGXBM", Zdlx.CHAR, 32, false, "父关系编码"));
        stgxFieldList.add(new TabeleField("XMBM", Zdlx.CHAR, 32, false, "项目编码"));
        stgxFieldList.add(new TabeleField("ZZDMC", Zdlx.VARCHAR, 50, false, "主字段名称"));
        stgxFieldList.add(new TabeleField("CZDMC", Zdlx.VARCHAR, 50, false, "从字段名称"));
        stgxFieldList.add(new TabeleField("SFSC", Zdlx.CHAR, 1, false, "是否删除"));
        stgxFieldList.add(new TabeleField("CJSJ", Zdlx.TIMESTAMP, null, false, "创建时间"));
        stgxFieldList.add(new TabeleField("ZHXGSJ", Zdlx.TIMESTAMP, null, false, "最后修改时间"));

    }

    public static Table getTable(String name) {
        return tableMap.get(name);
    }

    public static void createTable(GzyqService gzyqService) {
        createTable(gzyqService, null);
    }

    public static void createTable(GzyqService gzyqService, String tableName) {
        String databaseId = DataSourceConfig.getDatabaseId();
        Set<Map.Entry<String, Table>> entries = tableMap.entrySet();
        for (Map.Entry<String, Table> entry : entries) {
            String key = entry.getKey();

            if (tableName != null && !tableName.equalsIgnoreCase(key)) {
                continue;
            }
            Table table = entry.getValue();
            if ("dm".equalsIgnoreCase(databaseId)) {
                // 建表
                String dmCreateSql = table.getDmCreateSql();
                gzyqService.execute(dmCreateSql);

                // 建表注释
                String tableCommentSql = table.getDmTableCommentSql();
                gzyqService.execute(tableCommentSql);
                // 建字段注释
                List<TabeleField> fieldList = table.getFieldList();
                for (TabeleField tabeleField : fieldList) {
                    String commentSql = tabeleField.getDmCommentSql(table.getTableName());
                    gzyqService.execute(commentSql);
                }
            } else if ("mysql".equalsIgnoreCase(databaseId)) {
                String mysqlCreateSql = table.getMysqlCreateSql();
                gzyqService.execute(mysqlCreateSql);
            } else {
                throw new AppException("暂不支持该数据库---" + databaseId);
            }
        }
    }


    public static void dropTable(GzyqService gzyqService) {
        dropTable(gzyqService, null);
    }

    public static void dropTable(GzyqService gzyqService, String tableName) {
        String databaseId = DataSourceConfig.getDatabaseId();
        Set<Map.Entry<String, Table>> entries = tableMap.entrySet();
        for (Map.Entry<String, Table> entry : entries) {
            String key = entry.getKey();

            if (tableName != null && !tableName.equalsIgnoreCase(key)) {
                continue;
            }
            Table table = entry.getValue();
            if ("dm".equalsIgnoreCase(databaseId)) {
                String dmDropSql = table.getDmDropSql();
                gzyqService.execute(dmDropSql);
            } else if ("mysql".equalsIgnoreCase(databaseId)) {
                String mysqlDropSql = table.getMysqlDropSql();
                gzyqService.execute(mysqlDropSql);
            } else {
                throw new AppException("暂不支持该数据库---" + databaseId);
            }
        }
    }


    public static void truncateTable(GzyqService gzyqService) {
        truncateTable(gzyqService, null);
    }

    public static void truncateTable(GzyqService gzyqService, String tableName) {
        String databaseId = DataSourceConfig.getDatabaseId();
        Set<Map.Entry<String, Table>> entries = tableMap.entrySet();
        for (Map.Entry<String, Table> entry : entries) {
            String key = entry.getKey();

            if (tableName != null && !tableName.equalsIgnoreCase(key)) {
                continue;
            }
            Table table = entry.getValue();
            if ("dm".equalsIgnoreCase(databaseId)) {
                String dmTruncateSql = table.getDmTruncateSql();
                gzyqService.execute(dmTruncateSql);
            } else if ("mysql".equalsIgnoreCase(databaseId)) {
                String mysqlTruncateSql = table.getMysqlTruncateSql();
                gzyqService.execute(mysqlTruncateSql);
            } else {
                throw new AppException("暂不支持该数据库---" + databaseId);
            }
        }
    }

}
